package com.dyh.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.dyh.demo.dao.EmpMapper;
import com.dyh.demo.entity.Emp;
import com.dyh.demo.listenner.EmpListenner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @description emp控制器
* @author dyh
* @date 2022-10-14
*/
@Slf4j
@RestController
@RequestMapping("/emp")
public class EmpController {
    //path：为项目所在目录
    String path = "D:\\work\\useEasyExcel\\";


    @Autowired
    private EmpMapper empMapper;

    /**
     * 导出表
     * @return
     */
    @GetMapping("/export")
    private String export(){
        //将表中所有的数据导出
        //1.先拿出所有数据
        List<Emp> list = empMapper.selectList(null);
        //2.导出
        EasyExcel.write(path + "excel1.xlsx",Emp.class)
                .sheet("sheet1")
                .doWrite(list);
        return "ok";
    }

    /**
     * 导入表
     * @return
     */
    @GetMapping("/import")
    private String importExcel(){
        EasyExcel.read(path + "excel1.xlsx",Emp.class,new EmpListenner(empMapper))
                .sheet()
                .doRead();
        return "ok";
    }

}